package com.kaifamiao.coordinate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CoordinateTest2 {

    public static void main(String[] args) {

        ExecutorService es = Executors.newFixedThreadPool(2);

        final Object lock = new Object();

        Runnable task1 = new Runnable() {
            @Override
            public void run() {
                final String name = "孙悟空";
                synchronized (lock) {
                    try {
                        System.out.printf("[%s]即将释放同步锁\n", name);
                        lock.wait(); // 当前线程会释放同步锁进入等待池
                        System.out.printf("[%s]恢复运行\n", name);
                    } catch (InterruptedException e) {
                        System.out.printf("exception: %s\n", e.getMessage() );
                    }
                } // 同步代码块执行结束就会释放同步锁
            }
        };

        Runnable task2 = () -> {
            synchronized (lock) {
                final String name = "猪八戒";
                System.out.printf("[%s]即将唤醒其它线程\n", name);
                lock.notify();
                System.out.printf("[%s]已经唤醒其它线程\n", name);
            } // 同步代码块执行结束就会释放同步锁
        };

        es.submit( task1 );
        es.submit( task2 );

        es.shutdown();

    }

}
